package middle;

import com.eclipsesource.json.JsonArray;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 994. 腐烂的橘子
 */
public class OrangesRotting {


    public static int[] stringToIntegerArray(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if (input.length() == 0) {
            return new int[0];
        }

        String[] parts = input.split(",");
        int[] output = new int[parts.length];
        for(int index = 0; index < parts.length; index++) {
            String part = parts[index].trim();
            output[index] = Integer.parseInt(part);
        }
        return output;
    }

    public static int[][] stringToInt2dArray(String input) {
        JsonArray jsonArray = JsonArray.readFrom(input);
        if (jsonArray.size() == 0) {
            return new int[0][0];
        }

        int[][] arr = new int[jsonArray.size()][];
        for (int i = 0; i < arr.length; i++) {
            JsonArray cols = jsonArray.get(i).asArray();
            arr[i] = stringToIntegerArray(cols.toString());
        }
        return arr;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            int[][] grid = stringToInt2dArray(line);

            int ret = new Solution().orangesRotting(grid);

            String out = String.valueOf(ret);

            System.out.print(out);
        }
    }

    static class Solution {
        static int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

        public int orangesRotting(int[][] grid) {
            Queue<int[]> queue = new LinkedBlockingQueue<>();
            int m = grid.length,n=grid[0].length;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (grid[i][j] == 2) {
                        queue.offer(new int[]{i,j});
                    }
                }
            }
            int size = queue.size();
            int step = 0;
            while (!queue.isEmpty()){
                int[] point = queue.poll();
                size--;
                if (size<0){
                    step++;
                    size = queue.size();
                }
                for (int[] direction: dirs){
                    int ni = point[0]+direction[0];
                    int nj = point[1]+direction[1];
                    if (ni>=0&&ni<m&&nj>=0&&nj<n&&grid[ni][nj]==1){
                        grid[ni][nj]=2;
                        queue.offer(new int[]{ni,nj});
                    }
                }
            }

            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (grid[i][j] == 1) {
                        return -1;
                    }
                }
            }
            return step;
        }
    }
}
